//
//  DDRTPreViewViewController.m
//  DDRichTextDemo
//
//  Created by 张小伟 on 2021/9/18.
//

#import "DDRTPreViewViewController.h"
#import "CWRichGestureLabel.h"
#import "DDRTHeader.h"

// 行间距
#define KLineSpacing    10
// 字间距
#define kWordSpacing    2
// 字体大小
#define kTextFont       18

@interface DDRTPreViewViewController ()

@property(nonatomic,strong)CWRichGestureLabel *superTextLable;

@property(nonatomic,strong)UIImageView *fingerImageView;

@property(nonatomic,strong)NSTimer *timer;



@end

@implementation DDRTPreViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    CGFloat superTextLableY = kNavBarHeight + 50;
    self.superTextLable.frame = CGRectMake(10, superTextLableY, kScreenWidth - 10, kScreenHeight - superTextLableY);
    self.superTextLable.verticalAlignment = VerticalAlignmentTop;
//    self.superTextLable.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.superTextLable];
    
    
    
//    self.showConversationStr = @"最近在整理一个聊天的项目的时候，发送图片的时候，会有一个三角的指向效果，指向这张图片的发送者。服务端返回给我们的图片只是一张矩形的图片，我们如何把一张矩形的图片或者view，加上一层自定义遮罩效果，就是本文要讲的内容。效果演示如下：第一张是一个view的遮罩效果，我的们额方式";
    
    [self.superTextLable creatRichGestureLabel:self.showConversationStr andAttributes:[self getAllTextWordsAttributes] andShow:^(NSInteger index, BOOL isClick) {
        if (isClick == YES) {
//            DDLog(@"当前手指滑到了%ld行",index +1);
        }
    }];
    [self showFingerAnimationViews];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.timer invalidate];
    self.timer = nil;
}

- (void)dealloc
{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)showFingerAnimationViews {
    self.fingerImageView.bounds = CGRectMake(0, 0, 50, 50);
    [self.view insertSubview:self.fingerImageView aboveSubview:self.superTextLable];
    [self timerContinue:nil];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(timerContinue:) userInfo:nil repeats:YES];
    
}
-(void)timerContinue:(NSTimer *)timer {
    WS(ws);
//    DDLog(@"打印");
    ws.fingerImageView.center = CGPointMake(kScreenWidth * 0.5, - kNavBarHeight);
    [UIView animateWithDuration:2.5 animations:^{
        ws.fingerImageView.center = CGPointMake(kScreenWidth * 0.5, kScreenHeight + kNavBarHeight);
    } completion:^(BOOL finished) {
        ws.fingerImageView.center = CGPointMake(kScreenWidth * 0.5, - kNavBarHeight);
    }];
}

#pragma mark - Private Methods
-(NSDictionary *)getAllTextWordsAttributes {
    NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];
    muParagraph.lineSpacing = KLineSpacing; // 行距
    NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:kTextFont],NSKernAttributeName:@(kWordSpacing),NSParagraphStyleAttributeName:muParagraph};
    return dic;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.timer invalidate];
    self.timer = nil;
    
    [self.fingerImageView removeFromSuperview];
}

#pragma mark - Setter & Getter

-(CWRichGestureLabel *)superTextLable {
    if (!_superTextLable) {
        _superTextLable = [[CWRichGestureLabel alloc] init];
        _superTextLable.numberOfLines = 0;
        _superTextLable.userInteractionEnabled = YES;
        _superTextLable.textColor = [UIColor blackColor];
    
    }
    return _superTextLable;
}

-(UIImageView *)fingerImageView {
    if (!_fingerImageView) {
        _fingerImageView = [[UIImageView alloc] init];
        _fingerImageView.image = [UIImage imageNamed:@"Gesturefingerup"];
    }
    return _fingerImageView;
}


@end
